package de.home.spring.helper;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import de.home.spring.backend.IDAO;
import de.home.spring.beans.Game;
import de.home.spring.beans.Person;

public class ContextHolder {

	private static ApplicationContext context;
	private final static String contextFilePath = "src/main/webapp/WEB-INF/application-context.xml";
	
	private final static String personDAOBeanName = "personDAO";
	private final static String personDAOHBeanName = "personDAOH";
	
	private static void initialize() {
		if(context == null)
			context = new FileSystemXmlApplicationContext(contextFilePath);
	}
	
	public static Person getBeanPersonType(String beanName) {
		return getBean(beanName, Person.class);
	}
	
	public static Game getBeanGameType(String beanName) {
		return getBean(beanName, Game.class);
	}
	
	@SuppressWarnings("unchecked")
	public static IDAO<Person> getPersonDAO() {
		return getBean(personDAOBeanName, IDAO.class);
	}
	
	@SuppressWarnings("unchecked")
	public static IDAO<Person> getPersonDAOH() {
		return getBean(personDAOHBeanName, IDAO.class);
	}
	
	private static <T> T getBean(String beanName, Class<T> javaClass) {
		if(context == null)
			initialize();
		return (T) context.getBean(beanName, javaClass);
	}
	
}
